package dao;

import entity.Admin;
import util.DBUtil;
import java.sql.*;

public class AdminDao {

    public Admin login(String username, String password) {
        String sql = "SELECT * FROM admin WHERE username = ? AND password = ? AND status = 1";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, username);
            stmt.setString(2, password);

            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Admin admin = new Admin();
                    admin.setId(rs.getInt("id"));
                    admin.setUsername(rs.getString("username"));
                    admin.setName(rs.getString("name"));
                    admin.setPhone(rs.getString("phone"));
                    admin.setCreateTime(rs.getTimestamp("create_time"));
                    admin.setStatus(rs.getInt("status"));
                    return admin;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("登录��败", e);
        }
        return null;
    }

    public void updatePassword(Integer adminId, String newPassword) {
        String sql = "UPDATE admin SET password = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, newPassword);
            stmt.setInt(2, adminId);
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("修改密码失败", e);
        }
    }

    public Admin getAdminById(Integer id) {
        String sql = "SELECT * FROM admin WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setInt(1, id);

            try (ResultSet rs = stmt.executeQuery()) {
                if (rs.next()) {
                    Admin admin = new Admin();
                    admin.setId(rs.getInt("id"));
                    admin.setUsername(rs.getString("username"));
                    admin.setName(rs.getString("name"));
                    admin.setPhone(rs.getString("phone"));
                    admin.setCreateTime(rs.getTimestamp("create_time"));
                    admin.setStatus(rs.getInt("status"));
                    return admin;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("获取管理员信息失败", e);
        }
        return null;
    }

    public void updateAdminInfo(Admin admin) {
        String sql = "UPDATE admin SET name = ?, phone = ? WHERE id = ?";
        try (Connection conn = DBUtil.getConnection();
                PreparedStatement stmt = conn.prepareStatement(sql)) {

            stmt.setString(1, admin.getName());
            stmt.setString(2, admin.getPhone());
            stmt.setInt(3, admin.getId());
            stmt.executeUpdate();
        } catch (Exception e) {
            throw new RuntimeException("更新管理员信息失败", e);
        }
    }
}